<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Question as Questions;
use app\admin\model\Answer;
use think\Db;

class Question extends Common
{
    private $cModel;   //当前控制器关联模型
    
    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Questions;   //别名：避免与控制名冲突
    }
    public function edit($qid,$jid){
        if (request()->isPost()){
            $data = input('post.');
            if ($data['actions'] == 'adds'){    //增加答案
                $aModel = new answer();
                $result = $aModel->validate(CONTROLLER_NAME.'.qid')->allowField(true)->save($data);
            }            
            if ($result){
                return ajaxReturn(lang('action_success'), url('Question/edit',['qid'=>$qid,'jid'=>$jid]));
            }else{
                return ajaxReturn($this->aModel->getError());
            }
        }else{
            if ($qid > 0){
                $question =  $this->cModel->get($qid);
                $question['jid'] = $jid;
                $qModel = new answer();
                $answer = $qModel->where(['qid' => $qid])->select();
                $this->assign('question', $question);
                $this->assign('answer', $answer);
                return $this->fetch();
            }
        }
    }
    public function create($jid){
        if (request()->isPost()){
            Db::startTrans();
            try{
                $data = input('post.');
                $data['jid'] = $jid;
                
                $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
                $uid = $this->cModel->getLastInsID();
                // 提交事务
                if ($result && $uid){
                    Db::commit();
                    return ajaxReturn(lang('action_success'), url('Invest/index'));
                }else{
                    return ajaxReturn($this->cModel->getError());
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajaxReturn($e->getMessage());
            }
        }else{
            return $this->fetch('create');
        }
    }
    public function delete(){
        if (request()->isPost()){
            $id = input('id');
            
            if (isset($id) && !empty($id)){
                Db::startTrans();
                try{
                    $id_arr = explode(',', $id);   //问题数据
					$aModel = new Answer;
                    $where1 = [ 'qid' => ['in', $id_arr] ];
                    $where2 = [ 'id' => ['in', $id_arr] ];
                    $result = $this->cModel->where($where2)->delete();   //删除问题表数据
                    $result2 = $aModel->where($where1)->delete();   //删除对应答案库数据
                      
                    // 提交事务
                    if ($result){
                        Db::commit();
                        return ajaxReturn(lang('action_success'), url('Invest/index'));
                    }else{
                        return ajaxReturn($this->cModel->getError());
                    }
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return ajaxReturn($e->getMessage());
                }
            }
        }

    }
}